package com.cqengine.service.Impl;

import com.cqengine.entity.Position;
import com.cqengine.service.PositionService;
import com.googlecode.cqengine.TransactionalIndexedCollection;
import com.googlecode.cqengine.index.hash.HashIndex;
import com.googlecode.cqengine.index.unique.UniqueIndex;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.resultset.ResultSet;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.googlecode.cqengine.query.QueryFactory.*;

/**
 * @author tiger
 */
@Data
@Slf4j
@Service
public class PositionServiceTransactionalImpl extends PositionServiceImpl implements PositionService{


    public PositionServiceTransactionalImpl() {
        posQueue = new TransactionalIndexedCollection<>(Position.class);
        posQueue.addIndex(UniqueIndex.onAttribute(Position.ID));
        posQueue.addIndex(HashIndex.onAttribute(Position.PRICE));
    }

    @Override
    public Position query(String id){
        Query<Position> query = null;
        ResultSet<Position> retrieve = null;
        Position result = null;
        try {
            query = equal(Position.ID, id);
            retrieve = posQueue.retrieve(query);
            result = retrieve.iterator().next();
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("key is error");
        }finally {
            retrieve.close();
        }
        return result;
    }

    @Override
    public List<Position> query(double price){
        Query<Position> query = null;
        ResultSet<Position> retrieve = null;
        List<Position> result = null;
        try {
            query = equal(Position.PRICE, price);
            retrieve = posQueue.retrieve(query);
            result = IteratorUtils.toList(retrieve.iterator());
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("key is error");
        }finally {
            retrieve.close();
        }
        return result;
    }

    public List<Position> queryLessQuery(double price){
        Query<Position> query = null;
        ResultSet<Position> retrieve = null;
        List<Position> result = null;
        try {
            query = lessThanOrEqualTo(Position.PRICE, price);
            retrieve = posQueue.retrieve(query);
            result = IteratorUtils.toList(retrieve.iterator());
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("key is error");
        }finally {
            retrieve.close();
        }
        return result;
    }

    @Override
    public List<Position> query(String userId, String symbol) {
        Query<Position> query = null;
        ResultSet<Position> retrieve = null;
        List<Position> result = null;
        try {
            query = and(equal(Position.USERID, userId), equal(Position.SYMBOL, symbol));
            retrieve = posQueue.retrieve(query);
            result = IteratorUtils.toList(retrieve.iterator());
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("key is error");
        }finally {
            retrieve.close();
        }
        return result;
    }
 }
